package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.QuestionOptionMapper;
import com.tanhua.dubbo.mappers.SoulListMapper;
import com.tanhua.dubbo.mappers.SoulQuestionMapper;
import com.tanhua.dubbo.mappers.TestResultMapper;
import com.tanhua.model.domain.*;
import com.tanhua.model.vo.OptionsVo;
import com.tanhua.model.vo.SoulListVo;
import com.tanhua.model.vo.SoulQuestionVo;
import com.tanhua.service.SoulListApi;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author げっしょく
 * @date 2021/11/8
 */
public class SoulListApiImpl implements SoulListApi {

    @Autowired
    private SoulListMapper soulListMapper;

    @Autowired
    private SoulQuestionMapper soulQuestionMapper;

    @Autowired
    private QuestionOptionMapper questionOptionMapper;

    @Autowired
    private TestResultMapper testResultMapper;

    @Override
    public List<SoulListVo> getSoulListVo(Long userId) {
        //查询所有的测试列表
        List<SoulList> soulLists = soulListMapper.selectList(null);
        if (CollUtil.isEmpty(soulLists)) {
            return new ArrayList<>();
        }
        List<Long> soulListIDs = soulLists.stream().map(s -> s.getId()).collect(Collectors.toList());
        //通过列表id查询出对应的问题
        QueryWrapper qw = null;
        //列表下面所有的题目(列表id为key, 列表对应的题目为值)
        Map<Long, List<SoulQuestion>> soulquestionMap = new HashMap<>();
        //题目的所有id
        List<Long> questionIds = new ArrayList<>();
        for (Long soulListID : soulListIDs) {
            qw = new QueryWrapper();
            qw.eq("soulId", soulListID);
            List<SoulQuestion> list = soulQuestionMapper.selectList(qw);
            soulquestionMap.put(soulListID, list);
            for (SoulQuestion soulQuestion : list) {
                questionIds.add(soulQuestion.getId());
            }
        }
/*        qw.in("soulId", soulListIDs);
        List<SoulQuestion> list = soulQuestionMapper.selectList(qw);
        if (CollUtil.isEmpty(list)) {
            return new ArrayList<>();
        }

        List<Long> questionIds = list.stream().map(s -> s.getId()).collect(Collectors.toList());*/
        //通过问题id查询出所有的答案
        //题目下面所有的选线(题目id为key, 题目对应的选线为值)
        Map<Long, List<QuestionOption>> questionMap = new HashMap<>();
        for (Long questionId : questionIds) {
            qw.eq("question_id", questionId);
            List<QuestionOption> list1 = questionOptionMapper.selectList(qw);
            questionMap.put(questionId, list1);
        }

        //获取对应用户最近的一条测试数据
        qw = new QueryWrapper();
        qw.eq("userId", userId);
        qw.orderByDesc("created");
        List<TestResult> testResults = testResultMapper.selectList(qw);
        if (CollUtil.isEmpty(testResults)) {
            return new ArrayList<>();
        }
        //获取最近一条测试的id
        Long id = testResults.get(0).getId();

        List<SoulListVo> vos = new ArrayList<>();
        //遍历列表集合
        for (SoulList soulList : soulLists) {
            //通过列表集合获取对应的数据
            List<SoulQuestion> soulQuestions = soulquestionMap.get(soulList.getId());
            List<SoulQuestionVo> soulQuestionVos = new ArrayList<>();
            for (SoulQuestion soulQuestion : soulQuestions) {
                List<QuestionOption> questions = questionMap.get(soulQuestion.getId());
                if (!CollUtil.isEmpty(questions)) {
                    List<OptionsVo> o = new ArrayList<>();
                    for (QuestionOption question : questions) {
                        OptionsVo init = OptionsVo.init(question);
                        o.add(init);
                    }
                    SoulQuestionVo soulQuestionVo = SoulQuestionVo.init(soulQuestion, o);
                    soulQuestionVos.add(soulQuestionVo);
                }
            }
            SoulListVo vo = SoulListVo.init(soulList, soulQuestionVos, id.toString());
            vos.add(vo);
        }

        return vos;
    }
}
